<?php

namespace Gustavo\Hogar\UsuariosBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

use Gustavo\Hogar\UsuariosBundle\Entity\User;

class UsuarioController extends Controller
{
    public function indexAction()
    {   $name = "Juanito Perez";
        return $this->render('GustavoHogarUsuariosBundle:Usuario:index.html.twig', array('name' => $name)); 
    }
    
    /**
     * Método que permite registrar un Usuario
     */
    public function insertAction(){
        $content = $this->get("request")->getContent();
        $params = array();
        $user = new User();
        if (!empty($content))
        {
            $params = json_decode($content); 
            $user->setName($params->name);
            $user->setLastName($params->lastName);
            $user->setAddress($params->address);
            $user->setRegisterDate($params->registerDate);
            $user->setEnable($params->enable);
            
            /*Obtener isntancia de Doctrine*/
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
        }
        
        return new Response('Created user id '.$user->getId());
    }
    /**
     * Método que permite actualizar un USuario
     */
    public function updateAction(){
         $em = $this->getDoctrine()->getManager();
         $user = $em->getRepository('GustavoHogarUsuariosBundle:User')->find("1");
          if(!$user){
            throw $this->createNotFoundException('No User found for id 1');
          }
          $user->setName("Gustavo");
          $em->flush();
          return $this->redirect($this->generateUrl('GHAB_get', array('id'=> '1')));
    }
    /**
     * Métod que permite Eliminar un Usuario
     * @param type $id Identificador del Usuario
     */
    public function deleteAction($id){
        $em = $this->getDoctrine()->getManager();
         $user = $em->getRepository('GustavoHogarUsuariosBundle:User')->find($id);
          if(!$user){
            throw $this->createNotFoundException('No User found for id '.$id);
          }
          $em->remove($user);
          $em->flush();
    }
    /**
     * Método que permite realizar una búsqueda de Usuarios en base a Filtros
     */
    public function searchAction(){
        $repository = $this->getDoctrine()->getRepository('GustavoHogarUsuariosBundle:User');
        //$users = $repository->findBy(array('name'  => 'Sebastian'),array('id' => 'DESC'));
        $users = $repository->findAll();       
        $listadoUsuario = array();
        foreach ($users as $user) {
            $datosUsuario = array(
            "id"            => $user->getId(),
            "name"          => is_null($user->getName()) ? ''       : $user->getName(),
            "lastName"      => is_null($user->getLastName()) ? ''   : $user->getLastName(),
            "address"       => $user->getAddress(),
            "registerDate"  => $user->getRegisterDate(),
            "enable"        => $user->getEnable()     
            );
           $listadoUsuario[] =  $datosUsuario;
        }
         return new JsonResponse($listadoUsuario);
    }
    /**
     * Método que permite obttener un Usuario
     * @param type $id Identificador del Usuario
     */
    public function getAction($id){
          $em = $this->getDoctrine()->getManager();
          $user = $em->getRepository('GustavoHogarUsuariosBundle:User')->find($id);
          if(!$user){
            throw $this->createNotFoundException('No User found for id '.$id);
          }else{
           $datosUsuario = array(
            "id"            => $user->getId(),
            "name"          => is_null($user->getName()) ? ''       : $user->getName(),
            "lastName"      => is_null($user->getLastName()) ? ''   : $user->getLastName(),
            "address"       => $user->getAddress(),
            "registerDate"  => $user->getRegisterDate(),
            "enable"        => $user->getEnable()     
            );
           
            return new JsonResponse($datosUsuario);
          }
    }
}
